package rpc.client.socket;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.Socket;

import rpc.CommonStaticSource;

public class ClientSocket {
    public static final String IP_ADD = "127.0.0.1";
    private static Socket socket;
    private static ObjectOutputStream objectOutputStream;
    private static ObjectInputStream objectInputStream;
    
    static {
        try {
            socket = new Socket(IP_ADD, CommonStaticSource.SOCKET_PORT);
            objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
            objectInputStream = new ObjectInputStream(socket.getInputStream());
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
    
    public static synchronized Object oneRpcSericeMethodRun(Serializable obj) {
        Object result = null;
        try {
            objectOutputStream.writeObject(obj);
            System.out.println("client 已经写出");
            result = objectInputStream.readObject();
            System.out.println("client 读到数据");
            if(result != null){
                return result;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return result;
    }
    
}
